package cn.iocoder.ny.module.mall.service.impl;

import cn.iocoder.ny.module.mall.controller.admin.config.vo.MallConfigPageVo;
import cn.iocoder.ny.module.mall.controller.admin.favorites.dto.MallProductFavoriteDTO;
import cn.iocoder.ny.module.mall.controller.admin.favorites.vo.MallProductFavoritePageVo;
import cn.iocoder.ny.module.mall.domain.MallConfig;
import cn.iocoder.ny.module.mall.domain.MallUserFavorites;
import cn.iocoder.ny.module.mall.mapper.MallUserFavoritesMapper;
import cn.iocoder.ny.module.mall.service.IMallConfigService;
import cn.iocoder.ny.module.mall.service.IMallUserFavoritesService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * @author luchunhong
 * @date 2024/08/10/ 10:44
 * @description:
 */
@Service
public class IMallUserFavoritesServiceImpl extends ServiceImpl<MallUserFavoritesMapper, MallUserFavorites> implements IMallUserFavoritesService {
    @Autowired
    private MallUserFavoritesMapper mallUserFavoritesMapper;
    @Autowired
    private IMallConfigService mallConfigService;


    @Override
    public Integer addFavorites(MallUserFavorites mallUserFavorites) {
        LoginUser loginUser = getLoginUser();

        Long count = lambdaQuery().eq(MallUserFavorites::getProductId, mallUserFavorites.getProductId())
                .eq(MallUserFavorites::getTenantId, loginUser.getTenantId())
                .eq(MallUserFavorites::getUserId, loginUser.getId()).count();
        MallConfig one = mallConfigService.lambdaQuery().eq(MallConfig::getTenantId, loginUser.getTenantId()).one();
        mallUserFavorites.setMallId(one.getId());
        mallUserFavorites.setUserId(loginUser.getId());
        mallUserFavorites.setTenantId(loginUser.getTenantId());
        // 已收藏
        if (count > 0){
            return 0;
        }
        save(mallUserFavorites);
        return 1;
    }
    @Override
    public PageResult<MallProductFavoriteDTO> queryFavoritesList(MallProductFavoritePageVo reqVO) {
        IPage<MallProductFavoriteDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        mallUserFavoritesMapper.queryProductFavoritesList(page,reqVO);
        return new PageResult<>(page.getRecords(),page.getTotal());
    }

    @Override
    public Integer cancellationFavorite(MallUserFavorites mallUserFavorites) {
        int i = mallUserFavoritesMapper.deleteById(mallUserFavorites.getId());
        if (i == 0){
            return 0;
        }
        return 1;
    }

    @Override
    public MallUserFavorites queryFavorites(MallUserFavorites mallUserFavorites) {
        LoginUser loginUser = getLoginUser();
        if (!ObjectUtils.isEmpty(loginUser)){
            MallUserFavorites one = lambdaQuery().eq(MallUserFavorites::getProductId, mallUserFavorites.getProductId())
                    .eq(MallUserFavorites::getTenantId, loginUser.getTenantId())
                    .eq(MallUserFavorites::getUserId, loginUser.getId()).one();
            return one;
        }

        return null;
    }
}
